package com.itheima.web.servlet.store;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Product;
import com.itheima.utils.BeanUtil;
import com.itheima.web.servlet.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: My.zheng
 * @Description:
 * @Date: Created in 22:07  2020/11/29
 * @ModifiedBy:
 */
@WebServlet("/store/product")
/*/store/product?operation=findAll*/
public class ProductServlet extends BaseServlet{
   public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       int pageNum = 1;
       int pageSize = 5;
        if (StringUtils.isNoneBlank(request.getParameter("pageNum"))){
            pageNum= Integer.parseInt(request.getParameter("pageNum"));
        }
       if (StringUtils.isNoneBlank(request.getParameter("pageSize"))){
           pageSize= Integer.parseInt(request.getParameter("pageSize"));
       }
       PageInfo all = productService.findAll(pageNum, pageSize);
        request.setAttribute("page",all);

       request.getRequestDispatcher("/WEB-INF/pages/store/product/product-list.jsp").forward(request,response);
   }
   public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       List<Product> all = productService.findAll();
       HashSet set =new HashSet();
       for (Product product : all) {
           set.add(product.getCityName());
       }
       request.setAttribute("city",set);
       request.setAttribute("deptList",all);
       request.getRequestDispatcher("/WEB-INF/pages/store/product/product-add.jsp").forward(request,response);
   }
   public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
       Product product=new Product();
       Map<String, String[]> map = request.getParameterMap();
       ConvertUtils.register(new Converter() {
           @Override
           public Object convert(Class clazz, Object value) {
               //将String转化为date
               SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
               Date parse=null;
               try {

                   parse =format.parse(value.toString());
               } catch (ParseException e) {

                   e.printStackTrace();
               }
               return parse;
           }
       }, Date.class);

       BeanUtils.populate(product,map);

       productService.save(product);
      findAll(request, response);
   }
   public void toMany(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String id = request.getParameter("id");
       Product product = productService.findById(id);
       request.setAttribute("product",product);
       List<Product> all = productService.findAll();
       request.setAttribute("deptList",all);
       request.getRequestDispatcher("/WEB-INF/pages/store/product/product-many.jsp").forward(request,response);
   }

   public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

       String id = request.getParameter("id");
       Product product = productService.findById(id);
       request.setAttribute("product",product);
       List<Product> all = productService.findAll();
       HashSet set =new HashSet();
       for (Product p : all) {
           set.add(p.getCityName());
       }
       request.setAttribute("city",set);
       request.setAttribute("deptList",all);
       request.getRequestDispatcher("/WEB-INF/pages/store/product/product-update.jsp").forward(request,response);
   }
   public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       Product product = BeanUtil.fillBean(request, Product.class);
      productService.update(product);
      findAll(request, response);
   }
   public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       Product product = BeanUtil.fillBean(request, Product.class);
       productService.delete(product);
       findAll(request, response);
   }
    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String deleteArrays = "";
        try {
            deleteArrays = request.getParameter("deleteArrays");
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<String> productIds = new ObjectMapper().readValue(deleteArrays, new TypeReference<List<String>>() {
        });
        for (String ProductIds : productIds) {
            productService.deleteById(productIds);
        }
        response.sendRedirect(request.getContextPath() + "/store/product?operation=findAll");
    }

    public void dopen(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String openArrays = "";
        try {
            openArrays = request.getParameter("openArrays");
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<String> openIds = new ObjectMapper().readValue(openArrays, new TypeReference<List<String>>() {
        });
        for (String openId : openIds) {
            productService.dopen(openId);
        }
    }
    public void paralyse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String paralyseArrays = "";
        try {
            paralyseArrays = request.getParameter("paralyseArrays");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(paralyseArrays);
        List<String> paralyseIds = new ObjectMapper().readValue(paralyseArrays, new TypeReference<List<String>>() {
        });
        for (String paralyseId : paralyseIds) {
            productService.paralyse(paralyseId);
        }
    }

    public void FuzzyEnquiryTime(HttpServletRequest request, HttpServletResponse response) {

    }
    private void dateConvert() {
        ConvertUtils.register(new Converter() {
            public Object convert(Class type, Object value) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return simpleDateFormat.parse(value.toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }, Date.class);
    }

}
